package sanshi.contect.chinaOofresp.exer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ServletFilterDemo {
    public static void main(String[] args) {

        FilterChina china = new FilterChina();
        china.add(new OneFilter()).add(new TwoFilter()).add(new ThreeFilter());

        Request request = new Request();
        request.str = "request";
//        china.doFilter(request, null);

        Response response = new Response();
        response.str = "response";
        china.doFilter(request, response);



    }

}

class Request{
    String str ;
}
class Response{
    String str;
}

interface Filter{
    default boolean
    doFilter(Request request, Response response){return true;}
}

class OneFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response) {
         if (request != null){
             request.str = "this is first filter!";
             System.out.println(request.str);
             return true;
         }
         if (response != null ){
             response.str = "this is first filter!";
             System.out.println(response.str);
             return true;
         }
         return false;
    }
}
class TwoFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response) {
        if (request != null){
            request.str = "this is two filter!";
            System.out.println(request.str);
            return true;
        }
        if (response != null ){
            response.str = "this is two filter!";
            System.out.println(response.str);
            return true;
        }
        return false;
    }
}
class ThreeFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response) {
        if (request != null){
            request.str = "this is last filter!";
            System.out.println(request.str);
            return true;
        }
        if (response != null ){
            response.str = "this is last filter!";
            System.out.println(response.str);
            return true;
        }
        return false;
    }
}

/**
 * 责任链
 */
class FilterChina implements Filter{

    List<Filter> filters =  new ArrayList<>();

    public FilterChina add(Filter filter){
        filters.add(filter);
        return this;
    }

    @Override
    public boolean doFilter(Request request, Response response) {
        if (request != null) {
            for (Filter filter : filters) {
                if (!filter.doFilter(request, response)) {
                    return false;
                }
            }
        }
        if (response != null){
            ArrayList<Filter> filbac = new ArrayList<>();
            for (int i = filters.size(); i >0; i--){
                filbac.add(filters.get(i - 1));
            }
            for (Filter filter : filbac) {
                if (!filter.doFilter(request, response)) {
                    return false;
                }
            }
        }
        return true;
    }
}
